//
//  PikerImageTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/8/20.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "PikerImageTableViewCell.h"
#import "LCActionSheet.h"
#import "UUImageAvatarBrowser.h"
#import "CarpubMode.h"
@interface PikerImageTableViewCell ()<LCActionSheetDelegate>
@property (nonatomic,strong) UUImageAvatarBrowser *brower;
@end

@implementation PikerImageTableViewCell
+(instancetype)cellWithTablview:(UITableView *)tablview{
    PikerImageTableViewCell *cell=[tablview dequeueReusableCellWithIdentifier:@"image"];
    if (cell==nil) {
        [tablview registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] forCellReuseIdentifier:@"image"];
        cell=[tablview dequeueReusableCellWithIdentifier:@"image"];
    }
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
}
-(void)setMode:(CarpubMode *)mode{
    _mode=mode;
    self.titlelable.text=mode.name;
    IWUploadParam *param=_mode.param;
    if (param.image) {
        [self.pickerbtn setImage:param.image forState:UIControlStateNormal];
        self.pickerimage=param.image;
    }
}
-(void)setPickerimage:(UIImage *)pickerimage{
    _pickerimage=pickerimage;
    IWUploadParam *param=_mode.param;
    if (param) {
        param.image=_pickerimage;
    }
}

-(void)showImagepickerCv:(NSInteger)tag{
    if ([_delegate respondsToSelector:@selector(pickerBtnClick:WithBtn:)]) {
        self.pickerbtn.tag=tag;
        [_delegate pickerBtnClick:self WithBtn:self.pickerbtn];
    }
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    UITableView *tv = (UITableView *) self.superview;
    
    UITableViewController
    *vc = (UITableViewController *) tv.dataSource;
    [vc dismissViewControllerAnimated:YES completion:nil];
}



- (IBAction)pickerclick:(id)sender {
    if (self.pickerimage) {
        [self showhasactionsheet];
    }else{
        [self showactionsheet];}
   
}
-(void)showactionsheet{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles: @[@"拍照",@"从相册中选取"] redButtonIndex:-1 delegate:self];
    sheet.isTag=NO;
    [sheet show];
}
-(void)showhasactionsheet{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles: @[@"重新上传",@"查看大图"] redButtonIndex:-1 delegate:self];
    sheet.isTag=YES;
    [sheet show];
}

-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==2) {
        return;
    }
    if (actionSheet.isTag) {
        if (buttonIndex==0) {
            [self showactionsheet];
        }else{
            UUImageAvatarBrowser *brower=[[UUImageAvatarBrowser alloc]init];
            [brower showImage:self.pickerbtn.imageView];
            self.brower=brower;
        }
        
    }else{
        [self showImagepickerCv:buttonIndex];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
